cmake_minimum_required(VERSION 3.0)
project(live555)
SET(EXECUTABLE_OUTPUT_PATH ${live555_SOURCE_DIR}/bin)
SET(LIBRARY_OUTPUT_PATH ${live555_SOURCE_DIR}/lib)
SET(RUNTIME_OUTPUT_DIRECTORY ${live555_SOURCE_DIR}/bin)
OPTION(USING_SSL   "option for using openssla" ON)
OPTION(BUILD_TEST "build test programs or not" ON)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${live555_SOURCE_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${live555_SOURCE_DIR}/bin)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${live555_SOURCE_DIR}/bin)
add_compile_options( -O0 -g)
if(NOT DEFINED TARGET_SYSTEM)
    if(CMAKE_SYSTEM_NAME MATCHES "Linux")
        message(STATUS, "current platform: Linux.")
        add_compile_options(-DSOCKLEN_T=socklen_t -D_LARGEFILE_SOURCE=1 -D_FILE_OFFSET_BITS=64 -Wall -DBSD=1)
    elseif(CMAKE_SYSTEM_NAME MATCHES "Windows")
        message(FATAL_ERROR, "not support platform: Windows.")
    elseif(CMAKE_SYSTEM_NAME MATCHES "FreesBSD")
        message(STATUS, "curretn platfrom: FreesBSD.")
        add_compile_options(-DBSD=1 -DNEWLOCALE_NOT_USED=1 -DSOCKLEN_T=socklen_t -DHAVE_SOCKADDR_LEN=1 -Wall)
    elseif(CMAKE_SYSTEM_NAME MATCHES "Darwin")
        message("current platform: MacOS.")
        add_compile_options(-DBSD=1  -DSOCKLEN_T=socklen_t -DHAVE_SOCKADDR_LEN=1 -DTIME_BASE=int -DNEED_XLOCALE_H=1 -Wall)
    else()
        message(FATAL_ERROR, "Unknowm platform:${CMAKE_SYSTEM_NAME}.")        
    endif()
else()
    message(STATUS,"user set target system:${TARGET_SYSTEM}")
    if(TARGET_SYSTEM MATCHES "Linux")
        add_compile_options(-DSOCKLEN_T=socklen_t -D_LARGEFILE_SOURCE=1 -D_FILE_OFFSET_BITS=64 -Wall -DBSD=1)
    elseif(TARGET_SYSTEM MATCHES "Windows")
        message(FATAL_ERROR, "not support platform: Windows.")
    elseif(TARGET_SYSTEM MATCHES "FreesBSD")
        add_compile_options(-DBSD=1 -DNEWLOCALE_NOT_USED=1 -DSOCKLEN_T=socklen_t -DHAVE_SOCKADDR_LEN=1 -Wall)
    elseif(TARGET_SYSTEM MATCHES "QNX")
        add_compile_options("-D_QNX4 -DBSD -DSOCKLEN_T=uint32_t -WC -xs")
    else()
        message(FATAL_ERROR, "Unknowm platform:${TARGET_SYSTEM}.")
    endif()
endif()
if(USING_SSL)
#openssl required
    find_package(OpenSSL REQUIRED)
    SET(LOCAL_LIBS OpenSSL::SSL OpenSSL::Crypto)
    
else()    
    add_compile_options(-DNO_OPENSSL=1)
    message(STATUS,"Not use openssl")
    SET(LOCAL_LIBS socket)
endif()
set(
    LIVE555_INCLUDE_DIRS
    ${live555_SOURCE_DIR}/BasicUsageEnvironment/include
    ${live555_SOURCE_DIR}/groupsock/include    
    ${live555_SOURCE_DIR}/liveMedia/include
    ${live555_SOURCE_DIR}/UsageEnvironment/include
)
include_directories(
    /usr/local/include
    /usr/include
    ${LIVE555_INCLUDE_DIRS}
)
include_directories(
    ${OPENSSL_INCLUDE_DIR}
)
aux_source_directory(
    ./BasicUsageEnvironment
    BASIC_USAGE_ENVIRONMENT_SOURCE_FILES
)
aux_source_directory(
    ./liveMedia
    LIVE_MEDIA_SOURCE_FILES
)
aux_source_directory(
    ./mediaServer
    MEDIA_SERVER_SOURCE_FILES
)
aux_source_directory(
    ./groupsock
    GROUP_SOCK_SOURCE_FILES
)
aux_source_directory(
    ./UsageEnvironment
    USAGE_ENVIRONMENT_SOURCE_FILES
)
add_library(
    BasicUsageEnvironment
    STATIC
    ${BASIC_USAGE_ENVIRONMENT_SOURCE_FILES}
)
add_library(
    liveMedia
    STATIC
    ${LIVE_MEDIA_SOURCE_FILES}
)

add_library(
    groupsock
    STATIC
    ${GROUP_SOCK_SOURCE_FILES}
)
add_library(
    UsageEnvironment
    STATIC
    ${USAGE_ENVIRONMENT_SOURCE_FILES}
)
file(
    GLOB
    BASIC_USAGE_ENVIRONMENT_INCLUDES
    "BasicUsageEnvironment/include/*.h"
    "BasicUsageEnvironment/include/*.hpp"
    "BasicUsageEnvironment/include/*.hh"
    
)
file(
    GLOB
    GROUP_SOCK_INCLUDES
    "groupsock/include/*.h"
    "groupsock/include/*.hpp"
    "groupsock/include/*.hh"
)
file(
    GLOB
    LIVE_MEDIA_INCLUDES
    "liveMedia/include/*.h"
    "liveMedia/include/*.hpp"
    "liveMedia/include/*.hh"
    
)
set(MEDIA_SERVER_INCLUDES mediaServer/DynamicRTSPServer.hh)
file(
    GLOB
    USAGE_ENVIRONMENT_INCLUDES
    "UsageEnvironment/include*.h"
    "UsageEnvironment/include*.hpp"
    "UsageEnvironment/include*.hh"
)
set(
    LIVE555_HEAD_FILES
    ${BASIC_USAGE_ENVIRONMENT_INCLUDES}
    ${GROUP_SOCK_INCLUDES}
    ${MEDIA_SERVER_INCLUDES}
    ${USAGE_ENVIRONMENT_INCLUDES}
    ${LIVE_MEDIA_INCLUDES}
)
set(
    LIVE555_LIBS
    BasicUsageEnvironment
    groupsock
    liveMedia
    UsageEnvironment
)
# 编译testProgs
if(BUILD_TEST)
    set(
        LIVE555_LD_LIBS
	-Wl,--start-group
        BasicUsageEnvironment
        groupsock
        liveMedia
        UsageEnvironment
	-Wl,--end-group
    )
    add_executable(
        mediaServer
        ${MEDIA_SERVER_SOURCE_FILES}

    )

    target_link_libraries(
        mediaServer
        ${LIVE555_LD_LIBS}
        ${LOCAL_LIBS}
    )
    #build testProgs
    SET(OPEN_RESP_SRCS
        testProgs/openRTSP.cpp
        testProgs/playCommon.cpp
    )
    SET(PLAY_SIP_SRCS
        testProgs/playCommon.cpp
        testProgs/playSIP.cpp
    )
    set(COMMON_HDRS
        testProgs/playCommon.hh
    )
    set(HELPER_SRC
        testProgs/announceURL.cpp
        testProgs/announceURL.hh)
    add_executable(
        openRTSP
        ${COMMON_HDRS}
        ${OPEN_RESP_SRCS}
    )
    target_link_libraries(
        openRTSP
        ${LIVE555_LD_LIBS}
        ${LOCAL_LIBS}
    )
    add_executable(
        playSip
        ${COMMON_HDRS}
        ${PLAY_SIP_SRCS}
    )
    target_link_libraries(
        playSip
        ${LIVE555_LD_LIBS}
        ${LOCAL_LIBS}
    )
    add_executable(
        testAmrAudioStreamer
        ${COMMON_HDRS}
        testProgs/testAMRAudioStreamer.cpp
        ${HELPER_SRC}
    )
    target_link_libraries(
        testAmrAudioStreamer
        ${LIVE555_LD_LIBS}
        ${LOCAL_LIBS}
    )
    add_executable(
        testH264VideoStreamer
        ${COMMON_HDRS}
        testProgs/testH264VideoStreamer.cpp
        ${HELPER_SRC}
    )
    target_link_libraries(
        testH264VideoStreamer
        ${LIVE555_LD_LIBS}
        ${LOCAL_LIBS}
    )
    add_executable(
        testH265VideoStreamer
        ${COMMON_HDRS}
        testProgs/testH265VideoStreamer.cpp
        ${HELPER_SRC}
    )
    target_link_libraries(
        testH265VideoStreamer
        ${LIVE555_LD_LIBS}
        ${LOCAL_LIBS}
    )
    add_executable(
        testOnDemandRTSPServer
        ${COMMON_HDRS}
        testProgs/testOnDemandRTSPServer.cpp
        ${HELPER_SRC}
    )
    target_link_libraries(
        testOnDemandRTSPServer
        ${LIVE555_LD_LIBS}
        ${LOCAL_LIBS}
    )
    add_executable(
        testRTSPClient
        ${COMMON_HDRS}
        testProgs/testRTSPClient.cpp
    )
    target_link_libraries(
        testRTSPClient
        ${LIVE555_LD_LIBS}
        ${LOCAL_LIBS}
    )
    add_executable(
        testMP3Streamer
        testProgs/testMP3Streamer.cpp
        ${HELPER_SRC}
    )
    target_link_libraries(
        testMP3Streamer
        ${LIVE555_LD_LIBS}
        ${LOCAL_LIBS}
    )
    add_executable(
        testMP3Receiver
        testProgs/testMP3Receiver.cpp
        ${HELPER_SRC}
    )
    target_link_libraries(
        testMP3Receiver
        ${LIVE555_LD_LIBS}
        ${LOCAL_LIBS}
    )
    add_executable(
        testRelay
        testProgs/testRelay.cpp
    )
    target_link_libraries(
        testRelay
        ${LIVE555_LD_LIBS}
        ${LOCAL_LIBS}
    )
    add_executable(
        testReplicator
        testProgs/testReplicator.cpp
    )
    target_link_libraries(
        testReplicator
        ${LIVE555_LD_LIBS}
        ${LOCAL_LIBS}
    )
    add_executable(
        testH264VideoToHLSSegments
        testProgs/testH264VideoToHLSSegments.cpp
    )
    target_link_libraries(
        testH264VideoToHLSSegments
        ${LIVE555_LD_LIBS}
        ${LOCAL_LIBS}
    )
    add_executable(
        testMPEG1or2Splitter
        testProgs/testMPEG1or2Splitter.cpp
    )
    target_link_libraries(
        testMPEG1or2Splitter
        ${LIVE555_LD_LIBS}
        ${LOCAL_LIBS}
    )
    add_executable(
        testMPEG1or2VideoStreamer
        testProgs/testMPEG1or2VideoStreamer.cpp
        ${HELPER_SRC}
    )
    target_link_libraries(
        testMPEG1or2VideoStreamer
        ${LIVE555_LD_LIBS}
        ${LOCAL_LIBS}
    )
    add_executable(
        testMPEG1or2VideoReceiver
        testProgs/testMPEG1or2VideoReceiver.cpp
    )
    target_link_libraries(
        testMPEG1or2VideoReceiver
        ${LIVE555_LD_LIBS}
        ${LOCAL_LIBS}
    )
    add_executable(
        testMPEG1or2AudioVideoStreamer
        testProgs/testMPEG1or2AudioVideoStreamer.cpp
        ${HELPER_SRC}
    )
    target_link_libraries(
        testMPEG1or2AudioVideoStreamer
        ${LIVE555_LD_LIBS}
        ${LOCAL_LIBS}
    )
    add_executable(
        testMPEG2TransportStreamer
        testProgs/testMPEG2TransportStreamer.cpp
        ${HELPER_SRC}
    )
    target_link_libraries(
        testMPEG2TransportStreamer
        ${LIVE555_LD_LIBS}
        ${LOCAL_LIBS}
    )
    add_executable(
        testMPEG2TransportReceiver
        testProgs/testMPEG2TransportReceiver.cpp
    )
    target_link_libraries(
        testMPEG2TransportReceiver
        ${LIVE555_LD_LIBS}
        ${LOCAL_LIBS}
    )
    add_executable(
        testMPEG4VideoStreamer
        testProgs/testMPEG4VideoStreamer.cpp
        ${HELPER_SRC}
    )
    target_link_libraries(
        testMPEG4VideoStreamer
        ${LIVE555_LD_LIBS}
        ${LOCAL_LIBS}
    )
    add_executable(
        testDVVideoStreamer
        testProgs/testDVVideoStreamer.cpp
        ${HELPER_SRC}
    )
    target_link_libraries(
        testDVVideoStreamer
        ${LIVE555_LD_LIBS}
        ${LOCAL_LIBS}
    )
    add_executable(
        testWAVAudioStreamer
        testProgs/testWAVAudioStreamer.cpp
        ${HELPER_SRC}
    )
    target_link_libraries(
        testWAVAudioStreamer
        ${LIVE555_LD_LIBS}
        ${LOCAL_LIBS}
    )
    add_executable(
        testMKVStreamer
        testProgs/testMKVStreamer.cpp
        ${HELPER_SRC}
    )
    target_link_libraries(
        testMKVStreamer
        ${LIVE555_LD_LIBS}
        ${LOCAL_LIBS}
    )
    add_executable(
        testOggStreamer
        testProgs/testOggStreamer.cpp
        ${HELPER_SRC}
    )
    target_link_libraries(
        testOggStreamer
        ${LIVE555_LD_LIBS}
        ${LOCAL_LIBS}
    )
    add_executable(
        vobStreamer
        testProgs/vobStreamer.cpp
        ${HELPER_SRC}
    )
    target_link_libraries(
        vobStreamer
        ${LIVE555_LD_LIBS}
        ${LOCAL_LIBS}
    )
    add_executable(
        sapWatch
        testProgs/sapWatch.cpp
    )
    target_link_libraries(
        sapWatch
        ${LIVE555_LD_LIBS}
        ${LOCAL_LIBS}
    )
    add_executable(
        testMPEG1or2ProgramToTransportStream
        testProgs/testMPEG1or2ProgramToTransportStream.cpp
    )
    target_link_libraries(
        testMPEG1or2ProgramToTransportStream
        ${LIVE555_LD_LIBS}
        ${LOCAL_LIBS}
    )
    add_executable(
        testH264VideoToTransportStream
        testProgs/testH264VideoToTransportStream.cpp
    )
    target_link_libraries(
        testH264VideoToTransportStream
        ${LIVE555_LD_LIBS}
        ${LOCAL_LIBS}
    )
    add_executable(
        testH265VideoToTransportStream
        testProgs/testH265VideoToTransportStream.cpp
    )
    target_link_libraries(
        testH265VideoToTransportStream
        ${LIVE555_LD_LIBS}
        ${LOCAL_LIBS}
    )
    add_executable(
        MPEG2TransportStreamIndexer
        testProgs/MPEG2TransportStreamIndexer.cpp
    )
    target_link_libraries(
        MPEG2TransportStreamIndexer
        ${LIVE555_LD_LIBS}
        ${LOCAL_LIBS}
    )
    add_executable(
        testMPEG2TransportStreamTrickPlay
        testProgs/testMPEG2TransportStreamTrickPlay.cpp
    )
    target_link_libraries(
        testMPEG2TransportStreamTrickPlay
        ${LIVE555_LD_LIBS}
        ${LOCAL_LIBS}
    )
    add_executable(
        registerRTSPStream
        testProgs/registerRTSPStream.cpp
    )
    target_link_libraries(
        registerRTSPStream
        ${LIVE555_LD_LIBS}
        ${LOCAL_LIBS}
    )
    add_executable(
        testMKVSplitter
        testProgs/testMKVSplitter.cpp
    )
    target_link_libraries(
        testMKVSplitter
        ${LIVE555_LD_LIBS}
        ${LOCAL_LIBS}
    )
    add_executable(
        testMPEG2TransportStreamSplitter
        testProgs/testMPEG2TransportStreamSplitter.cpp
    )
    target_link_libraries(
        testMPEG2TransportStreamSplitter
        ${LIVE555_LD_LIBS}
        ${LOCAL_LIBS}
    )
    add_executable(
        mikeyParse
        testProgs/mikeyParse.cpp
    )
    target_link_libraries(
        mikeyParse
        ${LIVE555_LD_LIBS}
        ${LOCAL_LIBS}
    )
    #add_executable(
    #    testGSMStreamer
    #    testProgs/testGSMStreamer.cpp
    #    ${HELPER_SRC}
    #)
    #target_link_libraries(
    #    testGSMStreamer
    #    ${LIVE555_LIBS}
    #    ${LOCAL_LIBS}
    #)
endif()
add_subdirectory(examples)
message("INSTALL PATH:${CMAKE_INSTALL_PREFIX}")
# install static libraries and head files
install(
    TARGETS ${LIVE555_LIBS}
    ARCHIVE DESTINATION ${CMAKE_INSTALL_PREFIX}/lib
    #    LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/lib
)
install(
    FILES ${LIVE555_HEAD_FILES} DESTINATION ${CMAKE_INSTALL_PREFIX}/include/live555
)
